package com.coocaa.x.app.hall.parser.layoutparser;

import android.content.Context;

import com.coocaa.x.app.hall.parser.IParseInterface;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.generallayout.view.BaseFlipper.IPagerLifeCycleListener;
import com.coocaa.x.uipackage.generallayout.view.BasePageView;
import com.coocaa.x.uipackage.widget.SlideFocusView;

import java.util.List;

public class LayoutParser extends BasePageView implements IPagerLifeCycleListener,
        IParseInterface<LayoutParser> {

    protected Context mContext;
    protected HorizontalScrollCustomLayout recommenScroll;

    public enum LayoutType {
        HOME_PAGE,
        LIST_PAGE
    }

    public LayoutParser(Context context) {
        super(context);
        mContext = context;
        addScrollCustomLayout();
        // TODO Auto-generated constructor stub
    }

    public void initCustomLayout(LayoutType type) {
        BaseCustomLayout.CustomLayoutParams netAppParams = new BaseCustomLayout.CustomLayoutParams();
        if (type == LayoutType.HOME_PAGE) {
            netAppParams.mRow = 2;//hang
            netAppParams.mCol = 1000;
            netAppParams.mBlockWidth = CoocaaApplication.Div(305);//6
            netAppParams.mBlockHeight = CoocaaApplication.Div(305);//5
            netAppParams.mVerticalSpace = CoocaaApplication.Div(25);
            netAppParams.mHorizontalSpace = CoocaaApplication.Div(25);
            netAppParams.mMarginLeft = 0;
            netAppParams.mMarginTop = CoocaaApplication.Div(147);
            netAppParams.mBlockShadowX = CoocaaApplication.Div(0);
            netAppParams.mBlockShadowY = CoocaaApplication.Div(0);
        } else {
            netAppParams.mRow = 3;//hang
            netAppParams.mCol = 1000;
            netAppParams.mBlockWidth = CoocaaApplication.Div(340);//6
            netAppParams.mBlockHeight = CoocaaApplication.Div(195);//5
            netAppParams.mVerticalSpace = CoocaaApplication.Div(25);
            netAppParams.mHorizontalSpace = CoocaaApplication.Div(25);
            netAppParams.mMarginLeft = 0;
            netAppParams.mMarginTop = CoocaaApplication.Div(147);
            netAppParams.mBlockShadowX = CoocaaApplication.Div(0);
            netAppParams.mBlockShadowY = CoocaaApplication.Div(0);
        }

        refreshCustomLayout(netAppParams);
    }

    private void addScrollCustomLayout() {
        recommenScroll = creatCustomLayout();
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

        this.addView(recommenScroll, params);
    }

    private HorizontalScrollCustomLayout creatCustomLayout() {
        HorizontalScrollCustomLayout scroll = new HorizontalScrollCustomLayout(mContext);

        scroll.setFocusable(true);
        scroll.setFillViewport(false);
        scroll.setHorizontalScrollBarEnabled(false);
        return scroll;
    }

    private void refreshCustomLayout(BaseCustomLayout.CustomLayoutParams netAppParams) {
        recommenScroll.setCustomLayoutParams(netAppParams);
    }

    public void setOnScrollEndListener(HorizontalScrollCustomLayout.OnScrollEndListener listener) {
        recommenScroll.setOnScrollEndListener(listener);
    }

    public void setItemKeyEventListener(OnKeyListener listener) {
        recommenScroll.setmOnItemKeyEventListener(listener);
    }

    public void setItemFocusChangeListener(OnFocusChangeListener listener) {
        recommenScroll.setmOnItemFocusChangeListener(listener);
    }

    public void setOnItemClickListener(OnClickListener listener) {
        recommenScroll.setOnItemClickListener(listener);
    }

    public void setmFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        recommenScroll.setmFocusChangedEvent(focusChangedEvent);
    }

    public void setIItemViewLoadoverListener(
            BaseCustomLayout.IItemViewLoadOverListener listener) {
        recommenScroll.setIItemViewLoadoverListener(listener);
    }

    @Override
    public boolean onCovert() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onShow() {
        // TODO Auto-generated method stub

        return false;
    }

    @Override
    public boolean onDestroy() {
        // TODO Auto-generated method stub

        LogUtils.i("hall", "layoutParser destroy.");
        this.removeAllViews();
        if (recommenScroll != null)
            recommenScroll.onDestroy();
        recommenScroll = null;
        this.destroyDrawingCache();
        return true;
    }

    @Override
    public LayoutParser parse(String json) {
        // TODO Auto-generated method stub
        recommenScroll.setJson(json);

        return this;
    }

    List<String> jsonList = null;

    public LayoutParser parse(final List<String> jsonList) {
        // TODO Auto-generated method stub
        if (jsonList == null || jsonList.size() == 0) {
            return null;
        }
        this.jsonList = jsonList;

        recommenScroll.setJson(jsonList);
//		AsyncTask.runOnUiThread(new Runnable() {
//			@Override
//			public void run() {
//				recommenScroll.setJson(jsonList);
//			}
//		});
        return this;
    }
}
